package com.hbuzzs.springcloud.entities;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


@Getter
@Setter
@ToString(callSuper = true)
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler","statusCode"}, ignoreUnknown = true)
public class CommonResult<T> implements Response {
    private static final long serialVersionUID = -7443304902819898146L;

    private CommonResult(){}

    public static final int DEFAULT_OK = 20000;

    /**
     * [M] 平台状态码
     */
    @JsonProperty(index = 1)
    private int code = DEFAULT_OK;


    @JsonProperty(index = 2)
    private String msg;

    @JsonProperty(index = 3)
    private String dateRange;

    @JsonProperty(index = 4)
    private T data;




    public static CommonResult success(){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(MsgEnum.SUCCESS.code());
        commonResult.setMsg(MsgEnum.SUCCESS.message());
        return commonResult;
    }

    public static <T> CommonResult<T> success(MsgEnum msgEnum, T data){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(msgEnum.code());
        commonResult.setMsg(msgEnum.message());
        if(data != null){
            commonResult.setData(data);
        }
        return commonResult;
    }

    public static <T> CommonResult<T> success(MsgEnum msgEnum, BaseRequest baseRequest, T data){
    CommonResult<T> commonResult = new CommonResult<>();
    commonResult.setCode(msgEnum.code());
    commonResult.setMsg(msgEnum.message());

//    handleBaseRequest(restfulResponse, baseRequest);

    if(data != null){
        commonResult.setData(data);
    }
    return commonResult;
    }

	public static <T> CommonResult<T> success(MsgEnum msgEnum, String dateRange, T data){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(msgEnum.code());
        commonResult.setMsg(msgEnum.message());

        if (null!=dateRange){
            commonResult.setDateRange(dateRange);
        }

        if(data != null){
            commonResult.setData(data);
        }
        return commonResult;
    }




    public static CommonResult fail(MsgEnum msgEnum){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(msgEnum.code());
        commonResult.setMsg(msgEnum.message());
        return commonResult;
    }
    public static CommonResult fail(){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(MsgEnum.SERVER_ERROR.code());
        commonResult.setMsg(MsgEnum.SERVER_ERROR.message());
        return commonResult;
    }
    public static <T> CommonResult<T> fail(MsgEnum msgEnum, T data){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(msgEnum.code());
        commonResult.setMsg(msgEnum.message());
        if(data != null){
            commonResult.setData(data);
        }
        return commonResult;
    }


	/*private static void handleBaseRequest(RestfulResponse restfulResponse, BaseRequest baseRequest) {
		String timeGranularity = baseRequest.getTimeGranularity();
		String timeFrame = baseRequest.getTimeFrame();
		String returnDateType = baseRequest.getReturnDateType();
		//处理返回日期 时间粒度、时间范围、日期返回类型不得为空
		if (timeGranularity != null && !"".equals(timeGranularity)
				&& timeFrame != null && !"".equals(timeFrame)
				&& returnDateType != null && !"".equals(returnDateType)) {
			Map<String, Date> dateMap = DateUtil.getDateSection(baseRequest.getTimeGranularity(), baseRequest.getTimeFrame());
			if (dateMap != null) {
				Date startDate = DateUtil.add(dateMap.get(DateUtil.START_DATE), "D", 1);
				Date endDate = dateMap.get(DateUtil.END_DATE);
				Date date = dateMap.get(DateUtil.CURRENT_DATE);
				switch (baseRequest.getReturnDateType()) {
					case "1" :
						//1-    “mm/dd-mm/dd”
						restfulResponse.setDateRange(DateUtil.df4.format(startDate) + "-" + DateUtil.df4.format(endDate));

						break;
					case "2" :
						//2-    “yyyy/mm”
						restfulResponse.setDateRange(DateUtil.df5.format(endDate));

						break;
					case "3" :
						//3-    “mm”
						restfulResponse.setDateRange(DateUtil.df6.format(endDate));

						break;
					default:
						break;
				}
			}

		}*/


}
